<template>
  <el-dialog title="修改账户信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="头像：" prop="avatar">
        <new-upload
          :file_source="'avatar'"
          :upload-type="3"
          :format="'.jpg,.jpeg,.png,.JPG,.JPEG,.PNG'"
          :format-txt="'最多上传1张,支持格式：.jpg,.jpeg,.png,.JPG,.JPEG,.PNG'"
          :limit-num="1"
          @reUpload="getUpladImg"
          :fileArr="ruleForm.avatar"
        />
      </el-form-item>
      <el-form-item label="用户名：" prop="user_name">
        <el-input v-model="ruleForm.user_name"></el-input>
      </el-form-item>
      <el-form-item label="手机号：" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="eidtInfo">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import NewUpload from '@/components/NewUpload'
  import { editUesrInfo } from '@/api/user'
  export default {
    components: { NewUpload },
    data() {
      const validateusername = (rule, value, callback) => {
        if ('' == value) {
          callback(new Error('用户名不能为空'))
        } else if (!/^[a-zA-Z0-9\u4e00-\u9fa5]{6,}$/.test(value)) {
          callback(new Error('用户名不包含特殊字符，并不少于6位数'))
        } else {
          callback()
        }
      }
      return {
        ruleForm: {},
        dialogVisible: false,
        rules: {
          user_name: [{ required: true, trigger: 'blur', validator: validateusername }],
          phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
          avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }],
        },
      }
    },
    methods: {
      open(info) {
        this.ruleForm = { ...info }
        this.dialogVisible = true
        this.$nextTick(() => {
          this.ruleForm.avatar = this.ruleForm.avatar ? [this.ruleForm.avatar] : []
        })
      },
      eidtInfo() {
        console.log(this.ruleForm)
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.$confirm('确定修改账号信息?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            })
              .then(async () => {
                await editUesrInfo(this.ruleForm).then((data) => {
                  console.log('data: ', data)
                  localStorage.setItem('mall-admin-token', data.data.authorization)
                  if (data.code == 200) {
                    this.$message({
                      type: 'success',
                      message: '修改成功',
                    })
                    this.dialogVisible = false
                    this.$emit('getInfo')
                  } else {
                    this.$message({
                      type: 'error',
                      message: data.msg,
                    })
                  }
                })
              })
              .catch(() => {})
          } else {
            return false
          }
        })
      },
      handleClose() {
        this.$emit('close')
        this.dialogVisible = false
      },
      getUpladImg(file_list) {
        this.ruleForm.avatar = file_list
        console.log(' this.form.avatar : ', this.ruleForm.avatar)
      },
    },
  }
</script>

<style></style>
